<template>
  <el-card class="box-card">
    <template #header>账号列表</template>
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="account" label="账号" width="120">
      </el-table-column>
      <el-table-column prop="userGroup" label="用户组" width="150px">
      </el-table-column>
      <el-table-column label="日期" width="200px">
        <template v-slot="{ row }">
          {{ row.ctime | format }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200px">
        <template v-slot="{ row }">
          <el-button type="primary" size="mini" @click="edituser(row)"
            >编辑</el-button
          >
          <el-button type="danger" size="mini" @click="deluser(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 编辑用户 -->
    <div>
      <el-dialog title="编辑用户组" :visible.sync="dialogVisible" width="50%">
        <div>
          <el-form
            :model="eaitFrom"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="账号" prop="account">
              <el-input
                type="text"
                v-model="eaitFrom.account"
                autocomplete="off"
              ></el-input>
            </el-form-item>

            <el-form-item label="用户组" prop="usergrop">
              <el-select v-model="eaitFrom.usergrop" placeholder="请选择">
                <el-option label="超级管理员" value="超级管理员"> </el-option>
                <el-option label="普通管理员" value="普通管理员"> </el-option>
                <el-option label="垃圾管理员" value="垃圾管理员"> </el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editgay">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <div style="margin-top: 20px">
      <el-button @click="delALL()" type="danger">批量删除</el-button>
      <el-button @click="toggleSelection()" type="primary">取消选择</el-button>
    </div>
  </el-card>
</template>

<script>
import { accountLIst, delauser, delAll, editaccount } from '@/api/user.js'
import { validateaccount, validateusergrop } from '../../utils/rule'
import moment from 'moment'
export default {
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      arr: [],
      dialogVisible: false,
      // 数据对象
      eaitFrom: {
        account: '',
        usergrop: ''
      },
      // 规则对象
      rules: {
        account: [{ validator: validateaccount, trigger: 'blur' }],
        usergrop: [{ validator: validateusergrop, trigger: 'chang' }]
      }
    }
  },
  created() {
    this.getuser()
  },
  methods: {
    async getuser() {
      const { data, total } = await accountLIst({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      this.tableData = data
      this.total = total
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      this.currentPage = val
      this.getuser()
    },
    handleCurrentChange(val) {
      this.pageSize = val
      this.getuser()
    },
    deluser(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { code } = await delauser({ id })
          if (code === 0) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getuser()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    delALL() {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          this.multipleSelection.forEach(v => {
            const id = v.id
            this.arr.push(id)
          })
          if (this.arr.length === 0) {
            this.$message({
              type: 'error',
              message: '请选择内容再进行操作!'
            })
          } else {
            const { code } = await delAll({
              ids: JSON.stringify(this.arr)
            })
            if (code === 0) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getuser()
            }
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消批量删除'
          })
        })
    },
    // 数据回显
    edituser(row) {
      this.eaitFrom = { ...row }
      this.dialogVisible = true
    },
    // 修改账号
    async editgay() {
      this.dialogVisible = false
      const { account, usergrop, id } = this.eaitFrom
      const { code, msg } = await editaccount({
        id,
        account,
        userGroup: usergrop
      })
      if (code === 0) {
        this.$message.success(msg)
      } else {
        this.$message.error(msg)
      }
      this.getuser()
    }
  },
  filters: {
    format(str) {
      return moment(str).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style lang="less" scoped>
.el-input {
  width: 200px;
}
</style>
